package com.ta.gulimall.product.controller;

import com.ta.gulimall.common.utils.system.PageUtils;
import com.ta.gulimall.common.utils.system.R;
import com.ta.gulimall.product.service.AttrService;
import com.ta.gulimall.product.service.ProductAttrValueService;
import com.ta.gulimall.product.view.entity.ProductAttrValueEntity;
import com.ta.gulimall.product.view.vo.AttrVo;
import com.ta.gulimall.product.view.vo.AttrVoResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品属性
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    ProductAttrValueService productAttrValueService;
    /**
     * 分页属性信息
     * @param params
     * @param catelogId
     * @param attrType
     * @return
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R attrBaseList(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId, @PathVariable("attrType") String attrType){
        PageUtils page = attrService.pageAttrs(params, catelogId, attrType);

        return R.ok().put("page", page);
    }

    /**
     * 修改回显属性信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
        AttrVoResponse data = attrService.getAttr(attrId);

        return R.ok().put("attr", data);
    }

    /**
     * 保存属性信息
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改属性信息
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 回显spu属性信息
     */
    @RequestMapping("/base/listforspu/{spuId}")
    public R getSpuAttr(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> list = productAttrValueService.listSpuAttr(spuId);

        return R.ok().put("data", list);
    }


    /**
     * 修改sku属性值
     * @param spuId
     * @param entities
     * @return
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> entities){
        productAttrValueService.updateSpuAttr(spuId, entities);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
